//
//  MovieTableViewCell.m
//  WildDonkey
//
//  Created by 黎传炎 on 15/3/31.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "MovieTableViewCell.h"
#import "MovieData.h"
#import "UIImageView+WebCache.h"
@implementation MovieTableViewCell
- (void)dealloc
{
    [_Movie_image release];
    [_release_time release];
    [_user_name release];
    [_show_text release];
    [_MovieURl release];
    [_user_image release];
    [_image release];
    [_moviedata release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
        image1.layer.cornerRadius = 20;
        image1.layer.masksToBounds = YES;
        self.user_image = image1;
        [self.contentView addSubview:self.user_image];
        [image1 release];
        
        self.user_name = [[UILabel alloc]initWithFrame:CGRectMake(50, 0, 120, 30)];
        self.user_name.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.user_name];
        
        self.release_time = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, 100, 30)];
        self.release_time.font = [UIFont systemFontOfSize:10];
        [self.contentView addSubview:self.release_time];
        
        self.show_text =[[UILabel alloc]initWithFrame:CGRectMake(5, 30, self.bounds.size.width - 8, 300)];
        self.show_text.font = [UIFont systemFontOfSize:15];
        self.show_text.numberOfLines = 0;
        self.show_text.textAlignment = NSTextAlignmentNatural;
        [self.contentView addSubview:self.show_text];
        
        UIImageView *image2 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 150, self.bounds.size.width, 0)];
                               //WithFrame:CGRectMake(5, 150, self.bounds.size.width, 150)];
        self.Movie_image = image2;
        [self.contentView addSubview:self.Movie_image];
        [image2 release];
        
        _image = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, self.bounds.size.height + 50, 64, 64)];
        _image.image = [UIImage imageNamed:@"button"];
        [_Movie_image addSubview:_image];
        
    }
    return self;
}

- (void)setMoviedata:(MovieData *)moviedata
{
    if (_moviedata != moviedata) {
        [_moviedata release];
        _moviedata = [moviedata retain];
        
        _width = moviedata.width;//显示图片的宽度
        NSLog(@"width宽度：%d",_width);
        _height = (self.contentView.bounds.size.width - 20) * (moviedata.height) / _width;//显示图片的高度
        
        [_user_image setImageWithURL:[NSURL URLWithString:moviedata.profile_image]];
        
        NSString *str = [moviedata.create_time substringWithRange:NSMakeRange(0, 16)];
        _release_time.text = str;
        _release_time.textColor = [UIColor blueColor];
        
        _user_name.text = moviedata.name;
        _user_name.textColor = [UIColor purpleColor];
        
        _show_text.text = moviedata.text;
        self.show_text.frame = CGRectMake(5, 50, self.frame.size.width - 8, [MovieTableViewCell heightForString:moviedata.text]);
        
        [_Movie_image setImageWithURL:[NSURL URLWithString:moviedata.cdn_img]];
        //self.Movie_image.frame = CGRectMake(5, 50 + [MovieTableViewCell heightForString:moviedata.text] + 10, self.bounds.size.width - 10, 300);
        self.height = moviedata.height;
        
        
//        if (_Movie_image.frame.size.width < self.frame.size.width) {
//            
//        }
//        else
//        {
        
        if (moviedata.width >= [UIScreen mainScreen].bounds.size.width) {
            _Movie_image.frame = CGRectMake(5, 50 +[MovieTableViewCell heightForString:moviedata.text] + 10,[UIScreen mainScreen].bounds.size.width - 10,_height);
            NSLog(@"宽度是：%f",self.frame.size.width - 20);
            _image.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, _height / 2, 64, 64);
            
        }else{
            _Movie_image.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _width - 10)/2 + 10, 50 +[MovieTableViewCell heightForString:moviedata.text] + 10, _width - 10,moviedata.height);
            //NSLog(@"V电影with%f",_width - 10);
            NSLog(@"改为%d",_width - 10);
            _image.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, (moviedata.height)/2, 64, 64);
            
       // }
        }
        
        self.MovieURl = nil;
        self.MovieURl = moviedata.videouri;
        NSLog(@"视频的连接地址是：%@",_MovieURl);
        
    }
}

+ (CGFloat)heightForString:(NSString *)string
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName, nil];
    CGRect bounds = [string boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 8, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //NSLog(@"字体高度：%g",bounds.size.height);
    return bounds.size.height;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
